package com.yc.cloud.admin.controller;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.admin.dto.request.HwDeviceVideoRecordCreateReqDTO;
import com.yc.cloud.admin.dto.request.HwDeviceVideoRecordUpdateReqDTO;
import com.yc.cloud.admin.dto.request.HwDeviceVideoRecordPageReqDto;
import com.yc.cloud.admin.vo.HwDeviceVideoRecordVo;
import com.yc.cloud.admin.service.HwDeviceVideoRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

/**
 * <p>
 * 设备录像记录表 前端控制器
 * </p>
 *
 * @author kinggu
 * @since 2025-01-03
 */
@RestController
@Tag(name = "设备录像记录管理")
@RequestMapping("/hw-device-video-record")
@RequiredArgsConstructor
@Slf4j
public class HwDeviceVideoRecordController {

    @Resource
    private HwDeviceVideoRecordService hwDeviceVideoRecordService;

    @Operation(summary = "创建设备录像记录")
    @PostMapping
    public ResponseResult<Boolean> create(@Validated @RequestBody HwDeviceVideoRecordCreateReqDTO createReqDTO) {
        log.info("Creating video record: deviceId={}, stream={}", createReqDTO.getDeviceId(), createReqDTO.getStream());
        Boolean result = hwDeviceVideoRecordService.create(createReqDTO);
        return ResponseResult.success(result);
    }

    @Operation(summary = "更新设备录像记录")
    @PutMapping("/{id}")
    public ResponseResult<Boolean> update(
            @Parameter(description = "记录ID") @PathVariable("id") Long id,
            @Validated @RequestBody HwDeviceVideoRecordUpdateReqDTO updateReqDTO) {
        updateReqDTO.setId(id);
        log.info("Updating video record: id={}", id);
        Boolean result = hwDeviceVideoRecordService.update(updateReqDTO);
        return ResponseResult.success(result);
    }

    @Operation(summary = "删除设备录像记录")
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@Parameter(description = "记录ID") @PathVariable("id") Long id) {
        log.info("Deleting video record: id={}", id);
        Boolean result = hwDeviceVideoRecordService.delete(id);
        return ResponseResult.success(result);
    }

    @Operation(summary = "获取设备录像记录详情")
    @GetMapping("/{id}")
    public ResponseResult<HwDeviceVideoRecordVo> getById(@Parameter(description = "记录ID") @PathVariable("id") Long id) {
        HwDeviceVideoRecordVo result = hwDeviceVideoRecordService.getVOById(id);
        return ResponseResult.success(result);
    }

    @Operation(summary = "分页查询设备录像记录")
    @GetMapping
    public ResponseResult<CommonPage<HwDeviceVideoRecordVo>> list(@Validated HwDeviceVideoRecordPageReqDto pageReqDTO) {
        log.debug("Querying video records: deviceId={}, stream={}, pageNum={}, pageSize={}", 
                pageReqDTO.getDeviceId(), pageReqDTO.getStream(), pageReqDTO.getPageNum(), pageReqDTO.getPageSize());
        CommonPage<HwDeviceVideoRecordVo> result = hwDeviceVideoRecordService.list(pageReqDTO);
        return ResponseResult.success(result);
    }

    @Operation(summary = "根据设备ID查询录像记录")
    @GetMapping("/device/{deviceId}")
    public ResponseResult<CommonPage<HwDeviceVideoRecordVo>> listByDeviceId(
            @Parameter(description = "设备ID") @PathVariable("deviceId") Long deviceId,
            @Parameter(description = "页码") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        
        HwDeviceVideoRecordPageReqDto pageReqDTO = new HwDeviceVideoRecordPageReqDto();
        pageReqDTO.setDeviceId(deviceId);
        pageReqDTO.setPageNum(pageNum);
        pageReqDTO.setPageSize(pageSize);
        
        log.info("Querying video records by device: deviceId={}", deviceId);
        CommonPage<HwDeviceVideoRecordVo> result = hwDeviceVideoRecordService.list(pageReqDTO);
        return ResponseResult.success(result);
    }

    @Operation(summary = "根据流ID查询录像记录")
    @GetMapping("/stream/{stream}")
    public ResponseResult<CommonPage<HwDeviceVideoRecordVo>> listByStream(
            @Parameter(description = "流ID") @PathVariable("stream") String stream,
            @Parameter(description = "页码") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        
        HwDeviceVideoRecordPageReqDto pageReqDTO = new HwDeviceVideoRecordPageReqDto();
        pageReqDTO.setStream(stream);
        pageReqDTO.setPageNum(pageNum);
        pageReqDTO.setPageSize(pageSize);
        
        log.info("Querying video records by stream: stream={}", stream);
        CommonPage<HwDeviceVideoRecordVo> result = hwDeviceVideoRecordService.list(pageReqDTO);
        return ResponseResult.success(result);
    }

    @Operation(summary = "批量删除录像记录")
    @DeleteMapping("/batch")
    public ResponseResult<Boolean> batchDelete(@Parameter(description = "记录ID列表") @RequestBody java.util.List<Long> ids) {
        log.info("Batch deleting video records: ids={}", ids);
        
        if (ids == null || ids.isEmpty()) {
            return ResponseResult.failed("删除ID列表不能为空");
        }
        
        try {
            // 逐个删除以确保清理OSS文件资源
            for (Long id : ids) {
                hwDeviceVideoRecordService.delete(id);
            }
            return ResponseResult.success(true);
        } catch (Exception e) {
            log.error("Failed to batch delete video records: ids={}", ids, e);
            return ResponseResult.failed("批量删除失败: " + e.getMessage());
        }
    }
}
